//
//  RouterManager.swift
//  RouterPushProtocol
//
//  Created by Toj on 2/22/22.
//

/** 外部跳转app实现方式
 * 1. 路由注册, 跳转判断前缀host名称, 以前普遍
 * 2. 通过反射机制, 制定跳转url规则, ?前面是类名, ?后面就是参数, 通过分类"埋"进Controller内
 *    startimes://STHomeViewController?b=a
 * 3. 通过反射机制, 制定跳转url规则, ?前面是类名, ?后面就是参数, 参数不是"埋"进去, 而是通过KVC set 赋值给属性
 * 感觉2.3种比较简单, 而且效率, 个人喜欢第二种 逻辑清晰
 */

import UIKit

// 项目功能开关列表
public enum AppFBConfigType: Int {
    case `default`
}

open class AppFunctionManager {
    class func searching(type: AppFBConfigType) -> FunctionalState {
        return .usable
    }
}

// 项目功能开关列表
public enum FunctionalState: Int {
    case disapperDisable  //无效不显示
    case apperDisable // 有效可显示不能使用
    case usable
}

/** 路由跳转格式
 //startimes://STHomeViewController=a
 
 https://platformapi/webtoapp?
 
 Android:
 target=
 com.star.mobile.video.activity.BrowserActivity?loadUrl%3Dhttp://www.baidu.com%26isShare%3D%E4%B8%AD%E5%8D%88&channel=adjust&adjust_no_sdkclick=1
 
 iOS:
 &iOS_target=
 startimesApp://Product.STMyConinsViewController?parameter=value&....
 */
open class RouterManager: NSObject {
    static let shared = RouterManager()
    
    func push(url string: String) {
        let parameter: [String: String] = string.url_queryParameters ?? [:]
        let functional = parameter["functional"] as Any as? Int ?? 0
        let functionType = AppFBConfigType(rawValue: functional) ?? .default
        if functionType != .default {
            // 判断开关
            let f_state = AppFunctionManager.searching(type: functionType)
            if f_state == .disapperDisable {
                let message = parameter["message"] as Any as? String ?? ""
                print(message)
                // alert()
                return
            }
            
            if f_state == .apperDisable {
                let message = parameter["message"] as Any as? String ?? ""
                print(message)
                return
            }
        }
        
        let className = string.url_host
        
        // 反射类
        guard let vc = UIViewController.vc_reflection(className: className) else { return }
        vc.vc_reflection(parameter: parameter)
        
        UIViewController.vc_currentNavigation?.pushViewController(vc, animated: true)
    }
    
    func push(url: URL) {
        push(url: url.absoluteString)
    }
}
